﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using Models;

namespace SuperQuery.Converters
{
    /// <summary>
    /// Convert a QueryResult into a icon so that the user knows whether the query has run or failed.
    /// </summary>
    public class QueryResultToIconConverter : IValueConverter
    {
        /// <summary>
        /// Convert a QueryResult to an icon resource
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>An icon depending on the query canCloseApplication</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // Default to the wait image resource
            Uri result;
            result = new Uri("pack://application:,,,/Icons/Waiting.ico", UriKind.RelativeOrAbsolute);

            if ((value != null) && (value is QueryResult))
            {
                // We have QueryResult so see what 
                QueryResult qr = (QueryResult)value;
                if (qr.QuerySucceeded)
                {
                    result = new Uri("pack://application:,,,/Icons/Success.ico", UriKind.RelativeOrAbsolute);
                }
                else
                {
                    result = new Uri("pack://application:,,,/Icons/Fail.ico", UriKind.RelativeOrAbsolute);
                }
            }

            return result;
        }

        /// <summary>
        /// Converts th color back into a QueryResult. This function does nothing as databinding can only be one way for this conversion
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}
